// pages/goos_detail/goos_detail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    query:[],
    book:[],
    totalPrice:0,
    num: 0,
    bookprice: 0,
    userId: 0
  },
  getUser(){
    const user = wx.getStorageSync('user')
    console.log(user.username)
    let id=wx.getStorageSync('userId')
    console.log(id)
    wx.request({
      url: `http://localhost:8089/user/searchByUserName/${user.username}`,
      data: user.username,
      header: {
        'content-type': 'application/json'
      },
      method: 'GET',
      dataType: "json",
      success: (res)=>{
        console.log(res.data) 
        this.setData({
          userId: res.data.data.userId
        }) 
      },
    })
  },
  buybook(e){
    console.log(e)
    console.log(e.currentTarget.dataset)
    var orderitem = []
    orderitem.push(e.currentTarget.dataset);
    console.log(orderitem)
    wx.setStorageSync('orderitem', orderitem);
    wx.navigateTo({
      url: `/pages/pay/pay?bookId=${this.data.book.bookId}`,
    })
  },
  getBookNum(e){
    console.log(e.detail.value)
    this.setData({
      num: e.detail.value,
    })
  },
  getCartAdd(e){
    const user = wx.getStorageSync('user')
    console.log(e)
    console.log(user) 
    wx.request({
      url: 'http://localhost:8089/cart/list',
      data: {
        "bookId": e.detail.value.bookId,
        "price": e.detail.value.price,
        "quantity":e.detail.value.quantity,
        "userId": this.data.userId
      },
      header: {
        'content-type': 'application/json'
      },
      method: 'POST',
      dataType: "json",
      success: function (res){      
        console.log(res.data)
        wx.showToast({
          title: '加入购物车成功',
          icon: 'none'
        })
      },
    })
  },
  getBookDetails(){
    const user = wx.getStorageSync('user')
    console.log(this.data.query)
    wx.request({
      url: `http://localhost:8089/index/books/details/${this.data.query.bookId}`,
      method: 'GET',
      success: (res)=>{
        console.log(res.data)
        this.setData({
          book: res.data.data,
          bookprice:res.data.data.price
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      query: options
    })
    this.getBookDetails()
    this.getUser()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})